<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <!-- import CSS -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>

<style>
  .container1 {
    width: 100%;
    height: 100px;
    align-items: center;

    /*元素水平居中*/
    margin-top: 30px;
    margin-bottom: 30px;

  }

  .first {
    display: flex;
    flex-direction: row;
    text-align: left;
  }

  .name1 {
    font-size: 16px;
  }

  .name2 {
    font-size: 16px;
  }

  .name3 {
    font-size: 16px;
  }



  .button1 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .button2 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .button3 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .button4 {
    background-color: rgb(48, 43, 43);
    color: white;
  }

  .container2 {
    width: 100%;
    height: 600px;
    align-items: center;

    /*边框*/
    /*元素水平居中*/
  }

  .demo-ruleForm {
    width: 30%;
  }

  .block {
    text-align: center;
  }

  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

<body>

  <div id="app">
    <el-dialog title="电影修改" :visible.sync="alertUser" width="50%">
      <el-form :model="alertForm" ref="alertForm" label-width="100px">
        <el-form-item label="ID">
          <el-input v-model="alertForm.id" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="电影名称">
          <el-input v-model="alertForm.name"></el-input>
        </el-form-item>
        <el-form-item label="价格">
          <el-input maxlength="11" v-model="alertForm.price"></el-input>
        </el-form-item>

        <el-form-item label="简介" prop="info">
          <el-input v-model="alertForm.info"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="updatemovie" type="primary" size="small">保存 </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="电影isdn名称：">
        <el-input v-model="formInline.user" placeholder="电影isdn名称"></el-input>
      </el-form-item>
      <el-form-item label="电影批次：">
        <el-select v-model="formInline.groupid" placeholder="请选择">
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="searchuser">查询</el-button>
      </el-form-item>
    </el-form> -->
    <div class="container2">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="电影列表" name="first">
          <el-table ref="multipleTable" border :data="movieList" tooltip-effect="dark" style="width: 100%"
            @selection-change="handleSelectionChange">

            <el-table-column prop="postimg" label="电影海报" width="120">
              <template slot-scope="scope">
                <img :src="scope.row.postimg" alt="" style="width: 30px; height: 30px;">
              </template>
            </el-table-column>
            <el-table-column prop="name" label="电影名称" width="120">
            </el-table-column>
            <el-table-column prop="info" label="主题" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="price" label="电影价格" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="remain" label="剩余票数" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="sell" label="剩余票数" show-overflow-tooltip>
            </el-table-column>
            <el-table-column prop="sell" label="总票数" show-overflow-tooltip>
              <template slot-scope="scope">
                {{scope.row.sell + scope.row.remain}}
              </template>
            </el-table-column>

            <el-table-column prop="cateid" label="所属主题id" show-overflow-tooltip>

            </el-table-column>
            <el-table-column fixed="right" label="操作" width="120">
              <template slot-scope="scope">
                <el-button @click.native.prevent="deleteRow(scope.$index, movieList)" type="text" size="small">
                  删除
                </el-button>
                <el-button type="text" size="small" @click.native.prevent="alertRow(scope.$index, movieList)">
                  修改信息
                </el-button>
              </template>
            </el-table-column>
          </el-table>

        </el-tab-pane>
        <el-tab-pane label="添加电影" name="second">
          <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="海报" prop="name">
              <el-upload class="avatar-uploader" action="/photo/upload" :show-file-list="false"
                :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                <img v-if="imageUrl" :src="imageUrl" class="avatar">
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="名称" prop="name">
              <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="电影简介" prop="phone">
              <el-input v-model="ruleForm.info"></el-input>
            </el-form-item>
            <el-form-item label="电影价格">
              <el-input v-model="ruleForm.price"></el-input>
            </el-form-item>
            <el-form-item label="电影上映日期">
              <el-date-picker v-model="ruleForm.opentime" value-format="yyyy-MM-dd HH:mm:ss" type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="电影下线日期">
              <el-date-picker v-model="ruleForm.endtime" value-format="yyyy-MM-dd HH:mm:ss" type="date"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="总票量">
              <el-input v-model="ruleForm.remain"></el-input>
            </el-form-item>

            <el-form-item label="电影分类">
              <el-select v-model="ruleForm.cateid" placeholder="请选择">
                <el-option v-for="item in catelist" :key="item.id" :label="item.name" :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button @click="createmovie" type="success" size="small">创建信息 </el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>

</body>
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://cdn.bootcss.com/axios/0.19.2/axios.js"></script>

<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
  new Vue({
    el: '#app',
    data: {
      alertUser: false,
      alertForm: {
        id: 0,
        name: '',
        info: '',
        price: '',
       
      },
      ruleForm: {
        cateid: 1,
        name: '',
        price: 1,
        remain: 0,
        info: "",
        opentime: '',
        endtime: '',
        postimg: "",
      },
      formInline: {
        user: '',
        groupid: ''
      },

      activeName: 'first',
      movieList: [],
      multipleSelection: [],
      currentPage3: 5,
      imageUrl: ''
    },
    mounted() {
      this.getmovie();
      this.getcate();
    },
    methods: {
      getcate() {
        axios.post("/cate/getAllCate", this.alertForm).then(res => {
          console.log(res.data)
          if (res.data.result == "ok") {
            this.catelist = res.data.data
            console.log(res.data)
            return;
          }
        })
      },
      handleAvatarSuccess(res, file) {

        this.imageUrl = res;
        this.ruleForm.postimg = res;
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isLt2M) {
          this.$message.error('上传图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      updatemovie() {
        axios.post("/movie/updatemovie", this.alertForm).then(res => {
          console.log(res.data)
          if (res.data.result == "ok") {
            this.$notify.success({
              title: '成功',
              message: '修改信息成功'
            })
            this.alertUser = false;
            this.getmovie();
            console.log(res.data)
            return;
          }
        })
      },
      createmovie() {
        axios.post("/movie/addMovie", this.ruleForm).then(res => {
          console.log(res.data)
          let { result, data } = res.data;
          if (res.data.result == "ok") {
            this.getmovie();
            // this.movieList.push(data);
            this.$notify.success({
              title: '成功',
              message: '添加电影成功'
            })
            console.log(res.data)
            return;
          }
        })
      },
      searchuser() {
      },
      getmovie() {
        this.movieList = []
        axios.post("/movie/getAllMovie", {}).then(res => {
          console.log(res);
          if (res.data.result == "ok") {
            res.data.data.forEach(ele => {
              this.movieList.push(ele)
            })
          }

          console.log(this.userList)
        })
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleClick(tab, event) {

      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      handleDelete(index, row) {
        console.log(index, row);
      },
      deleteRow(index, rows) {
        axios.post("/movie/delMovie", { id: rows[index].id }).then(res => {
          console.log(res);
          if (res.data.result == "ok") {
            this.$notify.success({
              title: '删除成功',
              message: '删除成功！'
            })
            this.getmovie();
          }
        })
      },
      alertRow(index, row) {
        this.alertUser = true;
        this.alertForm.id = row[index].id;
        this.alertForm.name = row[index].name;
        this.alertForm.price = row[index].price;
        this.alertForm.info = row[index].info;
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
      }
    }
  })
</script>

</html>